package cz.mzk.funnylib.dao.db;

import cz.mzk.funnylib.dao.LibraryDAO;
import cz.mzk.funnylib.entities.Library;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author xrosecky
 */
public class LibraryDAOImpl implements LibraryDAO {

    private static String SELECT_LIBRARIES = "SELECT id, name, shortcut, street, postcode, city, lat, lon, web from libraries";
    private JdbcTemplate jdbcTemplate;

    public LibraryDAOImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Library> getLibraries() {
        return jdbcTemplate.query(SELECT_LIBRARIES, new LibraryMapper());
    }

    public void persist(Library library) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public class LibraryMapper implements RowMapper<Library> {

        @Override
        public Library mapRow(ResultSet rs, int rowNum) throws SQLException {
            Library library = new Library();
            library.setId(rs.getLong(1));
            library.setName(rs.getString(2));
            library.setShortcut(rs.getString(3));
            library.setStreet(rs.getString(4));
            library.setPostcode(rs.getString(5));
            library.setCity(rs.getString(6));
            library.setLat(rs.getString(7));
            library.setLon(rs.getString(8));
            library.setWeb(rs.getString(9));
            return library;
        }
    }
}
